/*    
    This file is part of the Passdroid password management software.
    
    Copyright (C) 2009-2010  Magnus Eriksson <eriksson.mag@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.kodholken.passdroid;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class DisplayPasswordActivity extends TimeoutActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.display_password);
		
		((TextView) findViewById(R.id.password)).setText(
								getIntent().getExtras().getString("password"));
		setupClipboardAction();
	}
	
	/**
	 * Set up a clickable password. When clicked the user will be prompted to
	 * choose if the password should be copied to the clipboard.
	 */
	private void setupClipboardAction() {
		final TextView passwordView = (TextView) findViewById(R.id.password);
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Copy to clipboard");
		alertDialog.setMessage("Copy the password to clipboard?");
		
		alertDialog.setButton(AlertDialog.BUTTON1, "Yes",
				             new DialogInterface.OnClickListener() { 
			public void onClick(DialogInterface dialog, int which) {
				ClipboardManager clipboard = 
					    (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				clipboard.setText(passwordView.getText());
			}
		});
		
		alertDialog.setButton(AlertDialog.BUTTON2, "No",
				              new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {}
		});
		
		passwordView.setOnClickListener(
			new OnClickListener() {
				@Override
				public void onClick(View v) {
					alertDialog.show();
				}
			}
		);
	}
}
